package com.android.baselibs.db;

import android.content.Context;
import android.util.Log;



import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by zouqh on 2018/4/19.
 */

public class XutilsDataDao implements XutlisDataImpl {


    private XutilsDB xutilsDB;
    private Context mContext;
    private static XutilsDataDao instance;


    protected XutilsDataDao(Context context) {
        this.mContext = context;

        xutilsDB = XutilsDB.getInstance(mContext);

    }


    public static XutilsDataDao getInstance(Context context) {
        if (instance == null) {
            synchronized (XutilsDataDao.class) {
                if (instance == null) {
                    instance = new XutilsDataDao(context);
                }
            }

        }
        return instance;
    }

    @Override
    public void insert(List beanArrayList) {
        try {
            xutilsDB.dbManager.save(beanArrayList);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insert(Object myBean) {
        try {
            xutilsDB.dbManager.save(myBean);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Object myBean) {
        try {
            xutilsDB.dbManager.saveOrUpdate(myBean);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(String columnName, String columnValue) {

    }

    @Override
    public void update(String queryColumnName, String queryColumnValue, String setColumnName, String setColumnValue) {

    }

    @Override
    public void delete(Object myBean) {
        try {
            xutilsDB.dbManager.delete(myBean);
        } catch (DbException e) {
            e.printStackTrace();
            Log.e("删除对象失败","delete---");
        }
    }

    @Override
    public void delete(String key, String value) {

    }

    @Override
    public int deleteAll() {
        return 0;
    }

    @Override
    public List query(String key, String value,Class clas) {
        try {
            return xutilsDB.dbManager.selector(clas).where(key, "=", value).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List query(String key, String value, String key1, String value1,Class clas) {
        try {
            return xutilsDB.dbManager.selector(clas).where(key, "=", value).and(key1, "=", value1).and("city", "=", "").findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long queryCount(Class clas) {
        return 0;
    }

    @Override
    public List queryAll(Class clas) {
        try {
            return xutilsDB.dbManager.selector(clas).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List queryAll(Class clas, String key) {
        try {
            return xutilsDB.dbManager.selector(clas).where("city", "=", "").orderBy(key,true).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object queryObj(Class clas, String key) {
        try {
//            System.out.println(xutilsDB.dbManager.selector(clas).orderBy(key,true).findFirst());
            return xutilsDB.dbManager.selector(clas).where("city", "=","").orderBy(key,true).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteTable(Class object) {
        try {
            xutilsDB.dbManager.dropTable(object);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
